package me.dr0n3.skyRanks.Commands;

import java.io.File;
import java.util.Arrays;
import me.dr0n3.skyRanks.Utils.IOHelper;
import me.dr0n3.skyRanks.Utils.TimeHelper;
import me.dr0n3.skyRanks.skyRanks;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * @author dr0n3
 * @date 20.04.2012
 */
public class playedTopCommand implements CommandExecutor
{
    skyRanks plugin;
    
    public playedTopCommand(skyRanks instance)
    {
        plugin = instance;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) 
    {
        if(!(sender instanceof Player))
            return false;
        
        Player player = (Player) sender;
        
        if(args.length == 0)
        {
            File dir = new File(IOHelper.mainDirectory + "/data/");
            File[] files = dir.listFiles();
            String[] names = new String[files.length];
            
            for(int i = 0; i < files.length; i++)
            {
                names[i] = files[i].getName().replaceAll(".txt", "");
            }
            
            long[] data = new long[names.length];
            
            for(int i = 0; i < names.length; i++)
            {
                data[i] = IOHelper.loadPlaytime(plugin.getServer().getPlayer(names[i]));
            }
            
            Arrays.sort(data);
            
            return true;
        }
        else
            return false;
    }
    
}
